#include "../../include/Dict/Chcommander.h"
#include<nlohmann/json.hpp>
#include<stdlib.h>
using json=nlohmann::json;
using std::istringstream;
Chcommander::Chcommander()
{
    ifstream ifs("../data/WordCount.txt");
    if (!ifs)
    {
        perror("ifs");
    }
    string line;
    string word;
    string number;
    while (getline(ifs, line))
    {
        istringstream iss(line);
        iss >> word;
        _dict[word];
        iss >> number;
        _dict[word] = atoi(number.c_str());
    }
}

string  Chcommander::recommander(const string &word){
    for(auto &e:_dict){//将编辑距离存储到_editdismap里面
        int editdis=editDistance(word,e.first);
        _editdismap.insert(make_pair(editdis,pair<int,string>(e.second,e.first)));
    }
    /* int cnt=5;//屏幕测试输出未统计频率只统计最短距离 */
    /* auto it=_editdismap.begin(); */
    /* cout<<"recommend word1 :"<<endl; */
    /* while(cnt-->0){ */
    /*     cout<<it->second.second<<endl; */
    /*     it++; */
    /* } */
    /* cout<<"-----------"<<endl; */


    int count=0;
    cout<<"recommend word :"<<endl;
    auto tmp=_editdismap.begin();
   vector<string> record;
    while(count<5){//抽取编辑距离最短的5个推荐词
    multimap<int,string,std::greater<int>> Five;
       auto itlow=_editdismap.lower_bound(tmp->first);//用来抽出距离相等频率最高的推荐词 
      auto ithigh=_editdismap.upper_bound(tmp->first);
      while(itlow!=ithigh){
            Five.insert(make_pair(itlow->second.first,itlow->second.second));
            itlow++;
      }
      auto mapit=Five.begin();
      while(count<5&&mapit!=Five.end()){
          cout<<mapit->second<<endl;//屏幕测试输出
          record.push_back(mapit->second);
          count++;
          mapit++;
      }
      tmp=ithigh;
    }
    json jsonMsg;
    jsonMsg["keywords"]=record;
    return jsonMsg.dump();
    /* int cnt=0; */
    /* auto it=_editdismap.begin(); */
    /* while(cnt<5){ */
    /*     cout<<it->second.second<<endl; */
    /*     it++; */
    /*     cnt++; */
    /* } */
    /* cout<<"----------"<<endl; */
   /* vector<string> record; */
    /* json jsonMsg; */
    /* jsonMsg["keyword"]=record; */
    /* return jsonMsg.dump(); */
    /* while(count<5){ */
    /*     record.push_back(it->second); */
    /*     cout<<"---"<<endl; */
    /*     cout<<it->second<<endl; */
    /*     it++; */
    /*     count++; */
    /* } */
//    return record;

}

/* string Chcommander::showReconmanderWord(){ */
/*     int count=0; */
/*     cout<<"recommend word :"<<endl; */
/*     auto tmp=_editdismap.begin(); */
/*    vector<string> record; */
/*     while(count<5){ */
/*     multimap<int,string,std::greater<int>> Five; */
/*        auto itlow=_editdismap.lower_bound(tmp->first); */ 
/*       auto ithigh=_editdismap.upper_bound(tmp->first); */
/*       while(itlow!=ithigh){ */
/*             Five.insert(make_pair(itlow->second.first,itlow->second.second)); */
/*             itlow++; */
/*       } */
/*       auto mapit=Five.begin(); */
/*       while(count<5&&mapit!=Five.end()){ */
/*           cout<<mapit->second<<endl; */
/*           record.push_back(mapit->second); */
/*           count++; */
/*           mapit++; */
/*       } */
      /* tmp=ithigh; */
    /* } */
    /* json jsonMsg; */
    /* jsonMsg["keyword"]=record; */
    /* return jsonMsg.dump(); */
        /* record.push_back(it->second); */
       /* // cout<<it->second<<endl; */
        /* it++; */
        /* count++; */
    /* } */
 //   return record;
/* } */
